package cn.audit.controller.log;

import cn.audit.aop.myannotation.BussAnnotation;
import cn.audit.bean.dto.BatchProcessing;
import cn.audit.bean.dto.UserCorrelation;
import cn.audit.bean.pojo.Project1;
import cn.audit.bean.pojo.User;
import cn.audit.bean.pojo.WorkLog;
import cn.audit.service.log.WorkLogService;
import cn.audit.service.operation.ProjectsItemService;
import cn.audit.utils.EmptyUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 李忆枫
 * @Date: 2019/5/23 17:26
 */
@Controller
@RequestMapping("/WorkLog")
public class WorkLogController {

    @Resource
    private WorkLogService workLogService;

    @Resource
    private ProjectsItemService projectsItemService;

    @RequestMapping("/toWorkLog")
    public String toWorkLog(Model model,
                            @RequestParam(value = "name", required = false) String name,
                            @RequestParam(value = "submissiondate", required = false) String submissiondate) {

        List<UserCorrelation> workLogs;
        if (EmptyUtils.isNotEmpty(submissiondate)) {
            workLogs = workLogService.findWorkLog(name, submissiondate);
        } else {
            workLogs = workLogService.findWorkLog(name, null);
        }

        List<Project1> project1s = projectsItemService.findAll();

        model.addAttribute("project1s", project1s);
        model.addAttribute("workLogs", workLogs);

        return "log/WorkLog";
    }

    @RequestMapping("/addWorkLog")
    @BussAnnotation(moduleName = "工作日志", type = "添加", option = "添加工作日志成功")
    public String addWorkLog(WorkLog workLog, HttpServletRequest request, String time) throws ParseException {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        workLog.setUserId(user.getId());
        workLog.setSubmissiondate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = sdf.parse(time);
        workLog.setWorkdate(workDate);
        workLogService.save(workLog);
        return "redirect:toWorkLog";
    }

    @ResponseBody
    @RequestMapping("/AjaxDelete")
    @BussAnnotation(moduleName = "工作日志", type = "刪除", option = "刪除工作日志成功")
    public Boolean AjaxDelete(BatchProcessing ids) {
        List<Long> id = ids.getIds();
        workLogService.batchDelete(id);
        return true;
    }

    @ResponseBody
    @RequestMapping("/AjaxWorkLogById")
    public Map<String, Object> AjaxWorkLogById(Long id) {
        //创建一个map，用来封装数据
        Map adminMap = new HashMap(16);
        WorkLog workLog = workLogService.findById(id);
        adminMap.put("workLog", workLog);
        return adminMap;
    }

    @RequestMapping("/updateWorkLog")
    @BussAnnotation(moduleName = "工作日志", type = "修改", option = "修改工作日志成功")
    public String updateWorkLog(WorkLog workLog, String time, String subDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date workDate = sdf.parse(time);
        Date submissionDate = sdf.parse(subDate);
        workLog.setWorkdate(workDate);
        workLog.setSubmissiondate(submissionDate);
        workLogService.save(workLog);
        return "redirect:toWorkLog";
    }

}
